package com.jxc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.entity.TbPermission;
import com.jxc.entity.TbPermissionRole;
import com.jxc.entity.TbRole;
import com.jxc.dao.TreeDAO;

/**
 * @包名   com.jxc.imp
 * @文件名 TreeImpl.java
 * @作者   mopzyqing
 * @创建日期 Jul 16, 2010
 * @版本 V 1.0
 */
@Transactional
public class TreeImpl implements TreeDAO{

	@Resource private SessionFactory sessionFactory;
	//获取一级菜单
	public List QueryMenu1() {
		
		String queryString="from TbMenu where parentId=0";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//根据菜单ID获取子菜单
	public List QueryMenuSubByID(int parentId) {
		
		String queryString="from TbMenu where parentId="+parentId+"order by orderby";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//根据权限的菜单ID获取权限
	 public List getOpByMenu(int menuId,int start,int limit){
		 
		 String queryString="from TbPermission where tbMenu.menuId="+menuId+"order by opId";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(
					queryString);
		queryObject.setFirstResult(start * limit);
		queryObject.setMaxResults(limit);
		return queryObject.list();
	 }
	//根据权限的菜单ID获取权限
	 public List getOpByMenuTable(int roleID,int menuId,int start,int limit){
		 
		 String queryString="select coalesce(role_id,0),tb_permission.op_id,tb_permission.op_name  from tb_permission left join tb_permission_role on tb_permission_role.role_id="+roleID+" and tb_permission_role.op_id=tb_permission.op_id where tb_permission.menu_id="+menuId+" order by tb_permission.op_id";
			Query queryObject = sessionFactory.getCurrentSession().createSQLQuery(
					queryString);
		queryObject.setFirstResult(start * limit);
		queryObject.setMaxResults(limit);
		return queryObject.list();
	 }
		//添加角色的权限
		public Boolean saveTbPermissionRole(String opArray[],int role_id) {
			
			try{
				Session session = sessionFactory.getCurrentSession();
				
				for(int i=0;i<opArray.length;i++){
					TbPermissionRole tbPermissionRole = new TbPermissionRole();
					tbPermissionRole.setOpValue(1);
					TbRole tbRole = new TbRole();
					tbRole.setRoleId(role_id);
					tbPermissionRole.setTbRole(tbRole);
					TbPermission tbPermission = new TbPermission();
					tbPermission.setOpId(Integer.valueOf(opArray[i].trim()));
					tbPermissionRole.setTbPermission(tbPermission);

					session.save(tbPermissionRole);
				}
			}catch(RuntimeException re){
				
				return false;
			}
			return true;
		}
		//根据角色id，删除该角色的权限
		public void deleteMenuSubByOrderby(int role_id,int menuId) {
			//String sql="delete from tb_permission_role where role_id="+role_id;
			String hql="from TbPermissionRole where tbRole.roleId="+role_id+" and tbPermission.tbMenu.menuId="+menuId;
			List list = sessionFactory.getCurrentSession().createQuery(hql).list();
			for(int i=0;i<list.size();i++){
				sessionFactory.getCurrentSession().delete((TbPermissionRole)list.get(i));
			}
		}
}
